#include "PluginManagerDialog.h"
#include "ui_PluginManagerDialog.h"

#include <QStandardItemModel>
#include <QStringListModel>
#include <QHeaderView>
#include <QDir>
#include <QFile>

PluginManagerDialog::PluginManagerDialog() :
    QDialog(),
    ui(new Ui::PluginManagerDialog)
{
    ui->setupUi(this);
}

bool PluginManagerDialog::ChoosePlugin(const QHash<QString, BasePlugin *> &plugins, QHash<QString, BasePlugin *>& choosedPlugin)
{

    //ui->PluginTree->header()->hide();
    this->setWindowTitle("Plugin Choose Widget");

    ui->PluginTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->PluginTree->header()->setSectionsMovable(true);

    QStandardItemModel model;
    connect(&model,&QStandardItemModel::itemChanged,this,[&](QStandardItem *item){
        if(item->data(Qt::UserRole + 1) == ChooseItemType::ChooseRoot){
            if(item->checkState() == Qt::Checked){
                int chlidcount = item->rowCount();
                for(int i = 0; i < chlidcount; i++){
                    item->child(i,0)->setCheckState(Qt::Checked);
                }
            }else{
                int chlidcount = item->rowCount();
                for(int i = 0; i < chlidcount; i++){
                    item->child(i,0)->setCheckState(Qt::Unchecked);
                }
            }
        }else if(item->data(Qt::UserRole + 1) == ChooseItemType::ChooseItem){
            if(item->checkState() == Qt::Checked){
                BasePlugin* baseplugin = plugins.value(item->text());
                choosedPlugin.insert(item->text(), baseplugin);
            }else{
                choosedPlugin.remove(item->text());
            }

        }
    });
    model.setColumnCount(2);
    model.setHeaderData(0,Qt::Horizontal,QString("Name"));
    model.setHeaderData(1,Qt::Horizontal,QString("Description"));

    QStandardItem* root1 = new QStandardItem("系统插件");
    root1->setData(ChooseItemType::ChooseRoot, Qt::UserRole + 1);
    root1->setCheckable(true);
    root1->setCheckState(Qt::Checked);

    QStandardItem* root2 = new QStandardItem("用户插件");
    root2->setData(ChooseItemType::ChooseRoot, Qt::UserRole + 1);
    root2->setEditable(false);
    root2->setCheckable(true);

    for(auto pair : plugins){
        QStandardItem* itemName = new QStandardItem(pair->GetName());
        itemName->setEditable(false);
        itemName->setCheckable(true);;
        itemName->setData(ChooseItemType::ChooseItem, Qt::UserRole + 1);

        QStandardItem* itemDescp = new QStandardItem(pair->GetDescription());
        itemDescp->setEditable(false);

        QList<QStandardItem*> row;
        row.push_back(itemName);
        row.push_back(itemDescp);
        if(pair->HasType(PluginType::System)){
            itemName->setCheckable(true);
            itemName->setCheckState(Qt::Checked);
            itemName->setEnabled(false);
            root1->insertRow(root1->rowCount(),row);
            choosedPlugin.insert(pair->GetName(),pair);
        }else if(pair->HasType(PluginType::User)){
            root2->insertRow(root2->rowCount(),row);
        }
    }

    model.insertRow(0,root1);
    model.insertRow(1,root2);

    ui->PluginTree->setModel(&model);

    if(this->exec() == Accepted){
        return true;
    }else{
        return false;
    }
}

PluginManagerDialog::~PluginManagerDialog()
{
    delete ui;
}

void PluginManagerDialog::on_btn_ok_clicked()
{
    this->accept();
}

void PluginManagerDialog::on_btn_cancel_clicked()
{
    this->reject();
}
